package com.yingxin.yxerp.module.activiti.biz.service.process;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.activiti.dto.AuditDto;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yxerp.module.activiti.api.BizProcessHandler;
import com.yingxin.yxerp.module.activiti.api.ProcessService;
import com.yingxin.yxerp.module.activiti.api.RevokeFinishDateHandler;
import com.yingxin.yxerp.module.activiti.biz.entity.RevokeFinishDateApply;
import com.yingxin.yxerp.module.activiti.biz.service.IRevokeFinishDateApplyService;
import com.yingxin.yxerp.module.activiti.biz.vo.resp.RevokeFinishDateApplyRespVo;
import com.yingxin.yxerp.module.activiti.entity.RevokeFinishEntity;
import com.yingxin.yxerp.module.organization.entity.User;
import com.yingxin.yxerp.module.organization.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Component
public class RevokeFinishDateApplyProcessHandler implements BizProcessHandler {

    @Autowired
    private ProcessService processService;

    @Autowired
    private IRevokeFinishDateApplyService revokeFinishDateApplyService;

    @Autowired
    private IUserService userService;

    @Autowired
    private RevokeFinishDateHandlerRouter revokeFinishDateHandlerRouter;

    /**
     * 启动流程实例
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void startProcessInstance(RevokeFinishDateApply apply) {
        // 检查是否满足撤销条件
        RevokeFinishDateHandler handler = revokeFinishDateHandlerRouter.getHandler(apply.getBusinessType());
        handler.check(apply.getBusinessId());
        // startProcessInstance
        processService.startProcessInstance(BusinessType.CXWJSQ, apply.getId(), apply.getApplyNo(), getVariables(apply.getId()));
    }


    @Override
    public Map<String, Object> getVariables(String businessId) {
        RevokeFinishDateApply apply = revokeFinishDateApplyService.getById(businessId);
        User creator = userService.getById(apply.getUserId());
        Map<String, Object> variables = new HashMap<>();
        if (StringUtils.isNotBlank(creator.getDeptId())) {
            variables.put("deptId", creator.getDeptId());
        }
        return variables;
    }

    @Override
    public void doCommonBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) {
        // update ApprovalStatus
        UpdateWrapper<RevokeFinishDateApply> uw = new UpdateWrapper<>();
        uw.lambda().set(RevokeFinishDateApply::getApprovalStatus, approvalStatus.getStatus());
        uw.lambda().eq(RevokeFinishDateApply::getId, id);
        revokeFinishDateApplyService.update(uw);
    }

    @Override
    public void doTaskApprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    /**
     * 业务方法：整个流程审核通过
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void doProcessApprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {
        RevokeFinishDateApplyRespVo apply = revokeFinishDateApplyService.mapper().getVo( id );
        // 完结
        RevokeFinishDateHandler handler = revokeFinishDateHandlerRouter.getHandler( apply.getBusinessType() );
        handler.finishDate( getRevokeFinishDateHandler( apply ) );
    }

    @Override
    public void doProcessUnapprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    /**
     * 获取-传递参数的对象
     * @param apply
     * @return
     */
    private RevokeFinishEntity getRevokeFinishDateHandler(RevokeFinishDateApplyRespVo apply ){

        RevokeFinishEntity revokeFinishEntity = new RevokeFinishEntity();
        // 业务单据id
        revokeFinishEntity.setBusinessId( apply.getBusinessId() );
        // 作废备注
        revokeFinishEntity.setFinishRemarks( apply.getReason() );
        // 作废单id
        revokeFinishEntity.setFinishID( apply.getId() );
        // 作废单编号
        revokeFinishEntity.setFinishNo( apply.getApplyNo() );
        // 作废申请人id
        revokeFinishEntity.setApplyUserId( apply.getUserId().toString() );
        // 作废申请人姓名
        revokeFinishEntity.setApplyUserName( apply.getApplicantName() );

        return revokeFinishEntity;
    }

}